<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <tree-tools :is-root="false" :obj="company" @add-dept="onAdd" />
        <el-tree
          :data="treeData"
          :default-expand-all="true"
          :props="defaultProps"
          @node-click="handleNodeClick"
        >
          <tree-tools
            slot-scope="{ data }"
            class="tree-tools"
            :obj="data"
            @add-dept="onAdd(data)"
            @edit-dept="onEdit"
          />
          v-model="dialogVisible"
        </el-tree>
      </el-card>
    </div>
    <add-dept ref="dept" v-model="dialogVisible" :node="treeNode" @add-success="loadDepartments" />
  </div>
</template>

<script>
import treeTools from './components/treeTools.vue'
import { getDepartmentInfo } from '@/api/department'
import { transListToTree } from '@/utils/index.js'
import AddDept from './components/addDept.vue'
export default {
  components: { treeTools, AddDept },
  data() {
    return {
      // 树形图数据
      treeData: [],
      defaultProps: {
        // children: 'child',
        label: 'name'
      },
      company: {
        name: '传智播客',
        manager: '负责人',
        id: ''
      },
      dialogVisible: false,
      treeNode: {}
    }
  },

  created() {
    this.loadDepartments()
  },

  methods: {
    async loadDepartments() {
      const res = await getDepartmentInfo()
      this.treeData = transListToTree(res.depts, '')
    },
    handleNodeClick() {},
    onAdd(data) {
      this.dialogVisible = true
      this.treeNode = data
    },
    onEdit(data) {
      // 显示弹层
      this.dialogVisible = true
      // 做校验
      this.treeNode = data
      this.$refs.dept.getDeptInfo(data.id)
    }
  }
}
</script>

<style scoped lang="scss">
.tree-tools {
  width: 100%;
}
</style>
